package com.kl.controller;


import com.kl.domain.UserDTO;
import com.kl.em.ConstantEnum;
import com.kl.em.Result;
import com.kl.entity.User;
import com.kl.entity.UserScore;
import com.kl.service.IUserScoreService;
import com.kl.service.IUserService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2021-10-29
 */
@RestController
@RequestMapping("userController")
public class UserController {

    @Autowired
    private IUserService userService;

    //用户表查询
    @ApiOperation(value = "用户表查询")
    @ApiImplicitParams({
            //name为方法中的参数名，value就是它的含义，required 是否必填
            @ApiImplicitParam(name = "userId",value = "用户ID",required = true,paramType = "query")
    })
    @GetMapping// @ResponseBody  将后台的数据以json的格式返回前台
    public Result<UserDTO> selectById(@RequestParam Long userId){// @RequestBody  将前端传入的json格式的数据绑定到实体类
        UserDTO userDTO = new UserDTO();
        try {
            //pageList = userMapper.selectPage(page, null);
            User user = userService.getById(userId);
            BeanUtils.copyProperties(user,userDTO);
        }catch (Exception e){
            e.printStackTrace();
            return Result.build(ConstantEnum.ERROR);
        }
        return Result.build(ConstantEnum.SUCCESS).setResult(userDTO);
    }

    //用户修改接口
    @ApiOperation(value = "用户表修改接口")
    @ApiImplicitParams({
            //name为方法中的参数名，value就是它的含义，required 是否必填
            @ApiImplicitParam(name = "user",value = "用户实体类",required = true,paramType = "body")
    })
    @PutMapping// @ResponseBody  将后台的数据以json的格式返回前台
    public Result updateInfo(@RequestBody UserDTO userDTO){// @RequestBody  将前端传入的json格式的数据绑定到实体类
        try {
            User user = new User();
            BeanUtils.copyProperties(userDTO,user);
            userService.updateById(user);
        }catch (Exception e){
            e.printStackTrace();
            return Result.build(ConstantEnum.ERROR);
        }
        return Result.build(ConstantEnum.SUCCESS);
    }
}
